/*
 * GTFSCalendarDates.java
 * 
 * Created by demory on Nov 12, 2009, 9:51:13 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.core.transit.gtfs;

import java.io.File;
import java.util.List;
import org.fpdev.core.transit.CalendarService;
import org.fpdev.core.transit.CalendarServices;

/**
 *
 * @author demory
 */
public class GTFSCalendarDates extends GTFSDBTable {

  public GTFSCalendarDates(File file, GTFSImporter importer) {
    super("calendar_dates", file, importer);
  }

  public void initCalendarDates(CalendarServices services) {
    int serviceIDCol = columnNames_.get("service_id");
    int dateCol = columnNames_.get("date");
    int exceptionTypeCol = columnNames_.get("exception_type");
    List<String[]> data = getData();
    for(String[] row : data) {
      String svcID = row[serviceIDCol];
      CalendarService service = services.getService(svcID);
      if(service == null) {
        service = new CalendarService(svcID, svcID);
        services.addService(service);
        System.out.println("new service: "+svcID);
      }
      String date = row[dateCol]; // YYYYMMDD format
      String dateFormatted = date.substring(4, 8) + date.substring(0, 4); // MMDDYYYY format

      int exceptionType = Integer.parseInt(row[exceptionTypeCol]);
      if(exceptionType == 1) service.newAddition(dateFormatted);
      if(exceptionType == 2) service.newSubtraction(dateFormatted);
    }
  }
}
